<script setup lang="ts">
import {markRaw, onMounted, ref} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import * as echarts from "echarts";
import {useGetBarDataApi} from "@/api";
import 'echarts-wordcloud'
import type {Bar} from "@/interface";

const bar = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const barData = ref<Bar>({
  name:[],
  value:[]
})
const init = async () => {
  const {data:res} = await useGetBarDataApi()
  barData.value = res
  myChart.value = markRaw(echarts.init(bar.value));
  draw();
}
const draw = () => {
  option.value = {
    title:{
      text:'各户型的不同楼层平均单价',
      left:'center'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: [
      {
        type: 'category',
        data: barData.value.name,
        axisTick: {
          alignWithLabel: true
        },
        axisLabel:{
          rotate:45
        }
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: [
      {
        name: barData.value.value[0].name,
        type: 'bar',
        data: barData.value.value[0].value
      },
      {
        name: barData.value.value[1].name,
        type: 'bar',
        data: barData.value.value[1].value
      },
      {
        name: barData.value.value[2].name,
        type: 'bar',
        data: barData.value.value[2].value
      }
    ]
  }
  myChart.value.setOption(option.value, {notMerge: true});
}
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="bar" class="bar">
  </div>
</template>

<style scoped>
.bar{
  width: 100%;
  height: 100%;
}
</style>